<?php
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . '/includes/cls_image.php');
$exc = new exchange($ecs->table('circle'), $db, 'id', 'circle_name');
$image = new cls_image($_CFG['bgcolor']);
/*------------------------------------------------------ */
//-- 框架
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
	admin_priv('circle_manage');
	//$smarty->assign('ur_here',      $_LANG['18_product_list']);
 	$smarty->assign('action_link',  array('href' => 'circle.php?act=add', 'text' => $_LANG['02_circle_add']));
 	
	$circle = get_circle_lists();
	$smarty->assign('circle_list',  $circle['row']);
	$smarty->assign('filter',       $circle['filter']);
	$smarty->assign('record_count', $circle['record_count']);
	$smarty->assign('page_count',   $circle['page_count']);
 	$smarty->assign('full_page',    1);
 	assign_query_info();
	$smarty->display("circle.htm");
}
/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif($_REQUEST['act']=="query"){
	$circle = get_circle_lists();
	$smarty->assign('circle_list',  $circle['row']);
	$smarty->assign('filter',       $circle['filter']);
	$smarty->assign('record_count', $circle['record_count']);
	$smarty->assign('page_count',   $circle['page_count']);
	make_json_result($smarty->fetch('circle.htm'), '',array('filter' => $circle['filter'], 'page_count' => $circle['page_count']));
}
elseif ($_REQUEST['act'] == 'edit_circle_name'){
// 	check_authz_json('store_list');
    $circle_id       = intval($_POST['id']);
    $circle_name         = $_POST['val'];

    if ($exc->edit("circle_name = '$circle_name'", $circle_id))
    {
        clear_cache_files();
        make_json_result($circle_name);
    }
}
elseif ($_REQUEST['act'] == 'edit_circle_order'){
    // 	check_authz_json('store_list');
    $circle_id       = intval($_POST['id']);
    $sort_order         = $_POST['val'];

    if ($exc->edit("sort_order = '$sort_order'", $circle_id))
    {
        clear_cache_files();
        make_json_result($sort_order);
    }
}
elseif ($_REQUEST['act'] == 'remove')
{
// 	check_authz_json('yyg_goods');
	$id = intval($_GET['id']);
	$exc->drop($id);
	$url = 'circle.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
	ecs_header("Location: $url\n");
	exit;
}
/****
 * 新增
 */
elseif($_REQUEST['act']=="add"){
	/* 权限判断 */
// 	admin_priv('yyg_goods');
	
	/* 初始化信息 */
	$smarty->assign('ur_here',      $_LANG['02_circle_add']);
	$smarty->assign('action_link',  array('text' => $_LANG['01_circle_list'], 'href'=>'circle.php?act=list'));
	$smarty->assign('form_action',  'insert');
	assign_query_info();
	$smarty->display('circle_info.htm');
}
elseif($_REQUEST['act']=="insert"){
	/* 权限判断 */
// 	admin_priv('yyg_goods');

	$info['circle_name'] = isset($_POST['circle_name'])?$_POST['circle_name']:"";
	$info['circle_desc'] = isset($_POST['circle_desc'])?$_POST['circle_desc']:"";
	
// 	if ((isset($_FILES['dirPath']['error']) && $_FILES['dirPath']['error'] == 0) || (!isset($_FILES['dirPath']['error']) && isset($_FILES['dirPath']['tmp_name'] ) &&$_FILES['dirPath']['tmp_name'] != 'none'))
// 	{
// 		$cat_icon = basename($image->upload_image($_FILES['dirPath'], 'afficheimg'));
		 
// 		$info['circle_img'] = $cat_icon;
// 	}
// 	if ((isset($_FILES['dirPath']['error']) && $_FILES['dirPath']['error'] > 0) || (!isset($_FILES['dirPath']['error']) && isset($_FILES['dirPath']['tmp_name']) && $_FILES['dirPath']['tmp_name'] == 'none'))
// 	{
// 		$link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
// 		sys_msg($_LANG['js_languages']['ad_photo_empty'], 0, $link);
// 	}
	
	
	$db->autoExecute($ecs->table('circle'), $info, 'INSERT');
	admin_log($_POST['circle_name'],'add','circle');
	$link[] = array('text' => $_LANG['back_list'], 'href'=>'circle.php?act=list&' . list_link_postfix());
	sys_msg($_LANG['edit_succeed'],0,$link);
}

elseif ($_REQUEST['act']=="edit"){
	/* 权限判断 */
// 	admin_priv('yyg_goods');

	$data        = get_circle_info($_REQUEST['id']);
	$smarty->assign('circle',               $data);
	$smarty->assign('ur_here',              $_LANG['03_circle_edit']);
	$smarty->assign('action_link',          array('text' => $_LANG['01_circle_list'], 'href'=>'circle.php?act=list&' . list_link_postfix()));
	$smarty->assign('form_action',        'update');
	assign_query_info();
	$smarty->display('circle_info.htm');

}
elseif ($_REQUEST['act'] == 'update')
{
	/* 权限判断 */
// 	admin_priv('yyg_goods');
	$info['circle_name'] = isset($_POST['circle_name'])?$_POST['circle_name']:"";
	$info['circle_desc'] = isset($_POST['circle_desc'])?$_POST['circle_desc']:"";
	
	if ((isset($_FILES['dirPath']['error']) && $_FILES['dirPath']['error'] == 0) || (!isset($_FILES['dirPath']['error']) && isset($_FILES['dirPath']['tmp_name'] ) &&$_FILES['dirPath']['tmp_name'] != 'none'))
	{
		$cat_icon = basename($image->upload_image($_FILES['dirPath'], 'afficheimg'));
		$info['circle_img'] = $cat_icon;
	}
	if ((isset($_FILES['dirPath']['error']) && $_FILES['dirPath']['error'] > 0) || (!isset($_FILES['dirPath']['error']) && isset($_FILES['dirPath']['tmp_name']) && $_FILES['dirPath']['tmp_name'] == 'none'))
	{
		$link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
		sys_msg($_LANG['js_languages']['ad_photo_empty'], 0, $link);
	}
	$db->autoExecute($ecs->table('circle'), $info, 'UPDATE', "id = '" . $_POST['id'] . "'");
	admin_log($_POST['circle_name'],'edit','circle');
	$link[] = array('text' => $_LANG['back_list'], 'href'=>'circle.php?act=list&' . list_link_postfix());
	sys_msg($_LANG['edit_succeed'],0,$link);
}
function get_circle_lists(){
	$result = get_filter();
	if ($result === false)
	{
		$where = " WHERE 1=1 ";
		/* 查询条件 */
		$filter['keywords']   = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
		if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
		{
			$filter['keywords'] = json_str_iconv($filter['keywords']);
		}
		$filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'id' : trim($_REQUEST['sort_by']);
		$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
	
		$where .= (!empty($filter['keywords'])) ? " AND circle_name like '%". mysql_like_quote($filter['keywords']) ."%'" : '';
	
		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('circle'). $where;
		$filter['record_count'] = $GLOBALS['db']->getOne($sql);
	
		$filter = page_and_size($filter);
	
		/* 获活动数据 */
		$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('circle') . $where.
				" ORDER by $filter[sort_by] $filter[sort_order] LIMIT ". $filter['start'] .", " . $filter['page_size'];
	
		$filter['keywords'] = stripslashes($filter['keywords']);
		set_filter($filter, $sql);
	}
	else
	{
		$sql    = $result['sql'];
		$filter = $result['filter'];
	}
	
	$row = $GLOBALS['db']->getAll($sql);
	$arr = array('row' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
	return $arr;
	
}

function get_circle_info($id){
	$where = " WHERE id='$id'";
	/* 获得活动数据 */
	$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('circle') . $where;
	$data = $GLOBALS['db']->GetRow($sql);
	$data['circle_img'] = "/data/afficheimg/".$data['circle_img'];
	return $data;
}